package com.bsj.sipgateway.web.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.bsj.power.common.config.exception.ApiException;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.config.global.JsonResultEnum;
import com.bsj.power.common.config.global.TokenUser;
import com.bsj.power.common.def.dto.job.SysJobDTO;
import com.bsj.power.common.def.entity.job.SysJob;
import com.bsj.power.common.util.StpLoginUserUtil;
import com.bsj.sipgateway.util.CronUtils;
import com.bsj.sipgateway.web.service.SysJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yinhao
 * @version 1.0
 * @description 调度任务操作
 * @date 2024/1/24
 */
@RestController
@RequestMapping(value = "/job")
public class SysJobController {

    @Autowired
    @Qualifier("sysJobServiceImpl")
    private SysJobService sysJobService;

    /**
     * 分页查询定时任务列表
     *
     * @param sysJob
     * @return
     */
    @GetMapping(value = "/list")
    public JsonResult<List<SysJob>> list(@Validated SysJobDTO sysJob) {
        return sysJobService.selectJobPageList(sysJob);
    }

    /**
     * 获取定时任务详细信息
     *
     * @param jobId
     * @return
     */
    @GetMapping(value = "/getInfo/{jobId}")
    public JsonResult<SysJob> getInfo(@PathVariable Long jobId) {
        return JsonResult.success(sysJobService.selectJobById(jobId));
    }

    /**
     * 新增定时任务
     *
     * @param job
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public JsonResult add(@RequestBody SysJobDTO job) throws Exception {
        if (!CronUtils.isValid(job.getCronExpression())) {
            throw new ApiException(JsonResultEnum.CRON_FORMAT_ERROR);
        }
        TokenUser tokenUser = StpLoginUserUtil.getTokenUser();
        if (tokenUser == null || tokenUser.getUserId() == null) {
            return JsonResult.fail(JsonResultEnum.LOGIN_EXIT);
        }
        job.setUserId(tokenUser.getUserId());
        return JsonResult.success(sysJobService.insertJob(job.change()));
    }

    /**
     * 修改定时任务
     */
    @PostMapping(value = "/edit")
    public JsonResult edit(@RequestBody SysJobDTO job) throws Exception {
        if (!CronUtils.isValid(job.getCronExpression())) {
            throw new ApiException(JsonResultEnum.CRON_FORMAT_ERROR);
        }
        return JsonResult.success(sysJobService.updateJob(job.change()));
    }

    /**
     * 定时任务状态修改
     *
     * @param job
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/changeStatus")
    public JsonResult changeStatus(@RequestBody SysJobDTO job) throws Exception {
        SysJob newJob = sysJobService.selectJobById(job.getJobId());
        newJob.setStatus(job.getStatus());
        return JsonResult.success(sysJobService.changeStatus(newJob));
    }

    /**
     * 定时任务立即执行一次
     *
     * @param job
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/run")
    public JsonResult run(@RequestBody SysJobDTO job) throws Exception {
        boolean result = sysJobService.run(job.change());
        return JsonResult.success(result);
    }

    /**
     * 删除定时任务
     *
     * @param jobIds
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/remove")
    public JsonResult remove(String jobIds) throws Exception {
        if (org.apache.commons.lang3.StringUtils.isBlank(jobIds)) {
            return JsonResult.fail(JsonResultEnum.PARAM_ERROR);
        }
        List<Long> ids = Arrays.stream(jobIds.split(",")).map(Long::parseLong).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(ids)) {
            return JsonResult.fail(JsonResultEnum.PARAM_ERROR);
        }
        sysJobService.deleteJobByIds(ids);
        return JsonResult.success();
    }

    /**
     * 绑定设备
     *
     * @param job
     * @return com.bsj.power.common.config.global.JsonResult
     * @author ljx
     * @time 2025/3/1 11:56
     */
    @PostMapping("/bindDevice")
    public JsonResult bindDevice(@RequestBody SysJobDTO job) {
        return sysJobService.bindDevice(job);
    }

}
